#include"DoubleListNode.h"
ListNode* ListCreate()
{
    ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
    //最初哨兵位的两个指针都指向自己
    phead->next = phead;
    phead->prev = phead;
    return phead;
}
ListNode* newNode(DateType x)
{
    ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
    newnode->next = NULL;
    newnode->prev = NULL;
    newnode->val = x;
    return newnode;
}
void Print(ListNode* phead)
{
  ListNode* cur = phead->next;//第一个节点
  while(cur != phead) //遍历回到哨兵位结束
  {
    printf("%d ",cur->val);
    cur = cur->next;
  }
  printf("\n");
}
void ListPushBack(ListNode* pHead, DateType x)
{
  assert(pHead);
  ListNode* tail = pHead->prev;
  ListNode* newnode = newNode(x); 
  //pHead tail newnode
  pHead->prev = newnode;
  tail->next = newnode;
  newnode->prev = tail;
  newnode->next = pHead;
}
void ListPopBack(ListNode* pHead)
{
  assert(pHead);
  assert(pHead->prev != pHead);
  ListNode* tail = pHead->prev;
  ListNode* prev = tail->prev;
  free(tail);
  prev->next = pHead;
  pHead->prev = prev;

}
void ListPushFront(ListNode* pHead, DateType x)
{
  assert(pHead);
  ListNode* newnode = newNode(x);
  ListNode* head = pHead->next;
  pHead->next = newnode;
  newnode->next = head;
  head->prev = newnode;
}
void ListPopFront(ListNode* pHead)
{
  assert(pHead && pHead->next != pHead);
  ListNode* head = pHead->next;
  ListNode* newhead =head->next;
  free(head);
  pHead->next = newhead;
  newhead->prev = pHead;
}
ListNode* ListFind(ListNode* pHead, DateType x)
{
  ListNode* cur = pHead->next;
  while(cur != pHead)
  {
    if(cur->val == x)
      return cur;
  }
  return NULL;
}
void ListAfterInsert(ListNode* pos, DateType x) //在pos之后插入
{
  assert(pos);
  //pos newnode posNext
  ListNode* newnode = newNode(x);
  ListNode* posNext = pos->next;
  pos->next = newnode;
  newnode->prev = pos;
  newnode->next = posNext;
  posNext->prev = newnode;
}
void ListErase(ListNode* pos)
{
  assert(pos&&pos->prev != pos);
  ListNode* posPrev = pos->prev; 
  ListNode* posNext = pos->next;
  free(pos);
  posPrev->next = posNext;
  posNext->prev = posPrev;
}
void ListDestory(ListNode** pHead) 
{
  ListNode* cur = (*pHead)->next;
  while(cur != (*pHead))
  {
    ListNode* next = cur->next;
    free(cur);
    cur = next;
  }
  free(*pHead);
  *pHead = NULL;
}
